package com.cqc.manage.controller.admin;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.cqc.manage.dto.JsonResultDto;
import com.cqc.manage.dto.RoleDto;
import com.cqc.manage.entity.Role;
import com.cqc.manage.service.Imp.RoleService;
import com.cqc.manage.util.JdbcTemplateSql;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色
 */
@RestController
@RequestMapping("/roles")
@Api(tags = "role", description = "角色相关的操作")
public class RoleController {

    private static Logger LOG = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private RoleService roleService;
    @Autowired
    private JdbcTemplateSql jdbcTemplateSql;

    /**
     * 添加角色
     *
     * @return
     */
    @ApiOperation(value = "添加角色", notes = "添加角色")
    @RequestMapping(
            value = "/addroles",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON addRole(@RequestBody Role role) {
        LOG.info("添加role用户请求{}", role);
        Role role1 = roleService.addRole(role);
        LOG.info("添加role用户结果:{}", role1);
        return JSONUtil.parse(new JsonResultDto<>("200", "添加成功"));
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    @ApiOperation(value = "更新角色", notes = "更新角色")
    @RequestMapping(
            value = "/updateroles",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON update(@RequestBody RoleDto role) {
        LOG.info("更新role用户请求{}", role);
        Role role1 = roleService.updateRole(role.getRoleId(), role.getRoleName());
        LOG.info("更新用户结果{}", role1);
        return JSONUtil.parse(new JsonResultDto<>("200", "修改成功"));
    }

    /**
     * 根据RoleId查询角色
     * @param role
     * @return
     */
    @ApiOperation(value = "根据roleId查询", notes = "根据roleId查询")
    @RequestMapping(
            value = "/findbyroleId",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON findRoleByRoleId(@RequestBody RoleDto role) {
        Role resultRole = roleService.selectById(role.getRoleId());
        LOG.info("根据RoleId查询结果{}", resultRole);
        return JSONUtil.parse(new JsonResultDto<Role>("请求成功", "200", resultRole));
    }

    /**
     * 根据roleName查询
     * @param role
     * @return
     */
    @ApiOperation(value = "根据roleName查询", notes = "根据roleName查询")
    @RequestMapping(
            value = "/findbyrolename",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON findRoleByRoleName(@RequestBody RoleDto role) {
        Role resultRole = roleService.selectByName(role.getRoleName());
        LOG.info("根据roleName查询结果{}", role);
        return JSONUtil.parse(new JsonResultDto<Role>("请求成功","200",resultRole));
    }

    /**
     * 获取全部role
     *
     * @return
     */
    @ApiOperation(value = "获取全部role", notes = "获取全部role")
    @RequestMapping(
            value = "/findallrole",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON findAllRole() {
        List<Role> allRole = roleService.getAllRole();
        LOG.info("获取全部role{}", allRole);
        List<RoleDto> allRoleDto = new ArrayList<>();
        for(Role role :allRole){
            RoleDto roleDto = new RoleDto();
            roleDto.setRoleName(role.getRoleName());
            roleDto.setRoleId(role.getRoleId());
            roleDto.setRoleRight(jdbcTemplateSql.RoleRigthList(role.getRoleId()));
            allRoleDto.add(roleDto);
        }
        return JSONUtil.parse(new JsonResultDto<List<RoleDto>>("success","200",allRoleDto));
    }
}
